﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class sign_up : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write("in sign_up_page"+Session["now_user_lang_no"].ToString());

        this.Session.Remove("backurl");

        this.DataBind();
        this.Label1.Visible = false;

        if (!this.IsPostBack)
        {
            this.CheckBoxList1.Items.FindByValue(Session["now_user_lang_no"].ToString()).Selected = true;

        }


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ctpDataContext db = new ctpDataContext();

        if (db.users.Count(p => p.email == this.TextBox1.Text) != 0)
        {
            this.Label1.Visible = true;
            return;
        }
        
        user myuser = new user();
        myuser.email = this.TextBox1.Text;

        string email = this.TextBox1.Text;
        email = email.Substring(0,email.IndexOf('@'));


  

        myuser.nickname = email;

        myuser.pwd = this.TextBox3.Text;
        myuser.addtime = DateTime.Now;
        myuser.profile_picture = "coming.gif";

        myuser.prefer_language_no = 0;

        db.users.InsertOnSubmit(myuser);
        db.SubmitChanges();

        myuser = db.users.Single(p => p.email == this.TextBox1.Text);

        foreach (ListItem i in this.CheckBoxList1.Items)
        {
            if (i.Selected)
            {
                user_language ul = new user_language();

                ul.language_no = int.Parse(i.Value);
                ul.userno = myuser.no;

                db.user_languages.InsertOnSubmit(ul);
                db.SubmitChanges();
            }

        }

        Response.Redirect("welcome.aspx");
    }
}
